/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.jabber.chat;

import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.json.JSONObject;
import org.webos.java.base.Dialog;
import org.webos.java.base.DialogType;
import org.webos.java.base.NetworkManager;
import org.webos.java.base.RemoteMessage;
import org.webos.java.jabber.JabberApp;
import org.webos.java.jabber.tree.ConnectionItem;
import org.webos.java.jabber.tree.GroupItem;
import org.webos.java.jabber.tree.UserItem;
import org.webos.java.root.Log;

/**
 *
 * @author Kostya
 */
@DialogType(jsClass="jabber.GroupMessage")
public class GroupMessage extends Dialog{

    private ConnectionItem connectionItem = null;
    private List<String> recipients = new ArrayList<String>();
    private JabberApp jabberApp = null;
    private String fromUserName = "";

    public GroupMessage(JabberApp app, GroupItem aGroupItem) {
        super(app);
        this.jabberApp = app;
        this.connectionItem = (ConnectionItem) aGroupItem.getParent();
        this.fromUserName = this.connectionItem.getUserName();
        StringBuffer buffer = new StringBuffer("Message to: ");
        for (int i = 0; i < aGroupItem.getChildrenCount(); i++) {
            UserItem userItem = aGroupItem.getChildren(i);
            if(i>0)
                buffer.append(", ");
            buffer.append(userItem.getUserName());
            recipients.add(userItem.getUserJID());
        }
        getData().putOpt("recipients", buffer.toString());
        getApplication().getSession().addDialog(this);
//        getData().putOpt("", app)
    }

    @RemoteMessage
    public JSONObject sendMessage(JSONObject aData){
        try {
            //Sending message here
            ConnectionItem item = jabberApp.getRosterFrame().getConnectionItem(fromUserName);
            for (String string : recipients) {
                if(jabberApp.getRosterFrame().sendMessage(fromUserName, string, aData.optString("message", ""))){
                try {
                    if(item!=null && item.isSaveHistory() && jabberApp.getConn().isActive()){
                        PreparedStatement st = jabberApp.getConn().prepareQuery("insert into history " +
                                "(account, contact, incoming, message_date, message) values " +
                                "(?, ?, ?, ?, ?)");
                        st.setString(1, fromUserName);
                        st.setString(2, string);
                        st.setBoolean(3, false);
                        st.setTimestamp(4, new Timestamp(new Date().getTime()));
                        st.setString(5, aData.optString("message", ""));
                        st.execute();
                        st.close();
                    }
                } catch (Throwable t) {
                    Log.error(this, "Exception thrown: " + t.getMessage());
                    t.printStackTrace();
                }
                    
                }else{
                    return NetworkManager.getErrorObject(810, "Can't send message. Network error");
                }
            }
            closeDialog(null);
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
            return NetworkManager.getErrorObject(810, "Can't send message. Network error");
        }
        return null;
    }
}
